/*
 * @Author: szx
 * @Date: 2022-03-18 14:20:13
 * @LastEditTime: 2022-03-18 14:37:59
 * @Description:
 * @FilePath: \leetcode\剑指offer\14\14II.JS
 */
/**
 * @param {number} n
 * @return {number}
 */
var cuttingRope = function (n) {
    const dp = new Array(n + 1).fill(BigInt(0));
    dp[2] = BigInt(1);
    const MOD = BigInt(1e9 + 7);
    for (let i = 3; i <= n; i++) {
        for (let j = 2; j < i; j++) {
            const max = dp[i - j] * BigInt(j) < (i - j) * j ? (i - j) * j : dp[i - j] * BigInt(j);
            dp[i] = dp[i] < max ? BigInt(max) : dp[i];
        }
    }
    return dp[n] % MOD;
};

var cuttingRope = function (n) {
    if (n < 4) return n - 1;
    
    let res = 1;
    while (n > 4) {
        res = (res * 3) % 1000000007;
        n -= 3;
    }
    return (res * n) % 1000000007;
};

console.log(cuttingRope(1000));
